package com.haust.leyou.service;

import com.haust.leyou.mapper.CategoryMapper;
import com.haust.leyou.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: csp1999
 * @Date: 2020/12/08/15:27
 * @Description:
 */
@Service
public class CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 根据父类目节点查询子类目节点内容
     *
     * @param pid
     * @return
     */
    public List<Category> queryCategoryByPid(Long pid) {
        Example example = new Example(Category.class);
        example.createCriteria().andEqualTo("parentId", pid);
        List<Category> list = categoryMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            throw new RuntimeException("查询到的分类集合为空...");
        }
        return list;
    }

    /**
     * 根据分类ids集合 得到商品分类Category集合的名称集合
     *
     * @param ids
     * @return
     */
    public List<String> queryNamesByIds(List<Long> ids) {

        List<Category> categoryList = categoryMapper.selectByIdList(ids);

        // 处理旧集合List<Category>返回一个新集合List<String>
        List<String> stringList = categoryList.stream().map(category -> {
            return category.getName();
        }).collect(Collectors.toList());

        return stringList;
    }
}
